package com.baturu.vin.benz.cleanData;

import com.baturu.vin.benz.BaseTest;
import com.baturu.vin.benz.cleanData.dao.BenzDealPartsDAO;
import com.baturu.vin.benz.dto.BenzDealPartsDTO;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author jiangxinlei
 * @Time 2017/8/10.
 */
public class BenzDealPartsDesc extends BaseTest {

    @Autowired
    private BenzDealPartsDAO benzDealPartsDAO;

    @Test
    public void dealPartsDesc() {
        List<String> allDescs = benzDealPartsDAO.getAllDescs();
        int maxId = benzDealPartsDAO.queryBmPartsMaxId();
        int max= 10000;
        for(int i=0; i<=maxId; i+= max) {
            System.out.println(new Date() + "开始执行第"+i+"条数据");
            List<BenzDealPartsDTO> partsDescs = benzDealPartsDAO.getPartsDesc(i, i + max);

            List<BenzDealPartsDTO> finalPartsDescs = Lists.newArrayList();
            for (BenzDealPartsDTO partsDesc : partsDescs) {
                String descidx = partsDesc.getDescidx();
                String[] splitDesc = splitString(descidx);
                String finalDesc = "";
                for (String s : splitDesc) {
                    if(allDescs.contains(s)) {
                        finalDesc = finalDesc + s;
                    }
                }
                finalPartsDescs.add(BenzDealPartsDTO.builder().descidx(finalDesc).id(partsDesc.getId()).build());
            }
            benzDealPartsDAO.updateDesc(finalPartsDescs);
            System.out.println("处理结束"+ new Date());
        }
    }

    private String[] splitString(String desc) {
        String regEx = "[MB]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(desc);
        String[] split = StringUtils.split(desc, "[MB]");
        for(int i =0 ; i<split.length ; i++) {
            if(m.find()) {
                split[i] = m.group() + split[i];
            }
        }
        return split;
    }

}
